<?php
class OrderAction extends Action{
  public function _initialize(){//初始化
		R("Home.Public","top");
		R("Home.Public","bottom");
   }
   public function index(){
	  if(empty($_SESSION["cart_pr"]))alert('购物车为空,无法结算,请先选购商品',U('Product/index')); 
	  
	  //求出所有省份
	  $sysregion = M("sysregion");
	  $province_list = $sysregion->where("region_type = 1")->order("id")->findAll();
	  $this->assign("province_list",$province_list);
	  
	  //求出北京所在的区域 
	  $parid = 2;
	  $first_city_list = $sysregion->where("parent_id = $parid and region_type =2")->order("id")->findAll();
	  $this->assign("first_city_list",$first_city_list);
	  
	  $this->assign("all_shop_price",$_GET["all_shop_price"]);
	  
	  $this->display();   
   }
   public function insert_order(){
	   
	   //产生订单号码
	   $_POST["order_sn"] = $this->create_order_code();
	   $_POST["order_status"] = "未处理";
	   $_POST["sum_fee"] = $_POST["all_shop_price"];
	   
	   $userorder = M("userorder");
	   
	   
	   if($vo = $userorder->create()){
		  $list = $userorder->add();
		  if(false === $list)$this->redirect("error");   
	   }else{
		   alert("出现错误");
	   }
	   
	   //保存订单详细
	   $orderdetail = M("orderdetail");
	   $cart_pr = $_SESSION["cart_pr"];
	   $data["order_sn"] =  $this->create_order_code();
	   foreach($cart_pr as $k=>$v){
		     if($v['pr_info']['pr_type']=='combpr'){//套餐
				 $data["pr_id"] = 0;
				 $data["comb_id"]=$v['pr_id'];
				 $data["pr_type"]="套餐";
			 }else{
				 $data["pr_id"] = $v['pr_id'];
				 $data['comb_id']=0;
				 $data["pr_type"]="普通";
			 }
		     
			 $data["pr_num"] = $v['buy_num'];
			 $data["pr_shop_price"] = $v['pr_info']['pr_shop_price'];
			 $orderdetail->add($data);
	   }
	   unset($_SESSION["cart_pr"]);
	   unset($_SESSION["order_code"]);
	   $this->redirect("success");
   }
   public function success(){
	   $this->display();
   }
   public function error(){
	   $this->display();   
   }
   public function get_city_list(){
	  checkget();
	  $p_id = $_GET["p_id"];
	  $sysregion = M("sysregion");
	  
	  $city_list = $sysregion->where("parent_id = $p_id and region_type =2")->order("id")->findAll();
	  echo json_encode($city_list);     
   }
   public function create_order_code(){
	  if(isset($_SESSION["order_code"])) return $_SESSION["order_code"];
	  
	  $code = date("YmdHis").mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
	  $_SESSION["order_code"] = $code;
	  
	  return $_SESSION["order_code"];
   }
	
}
?>